package com.cxs.musicplaywidget.player.store;

import ohos.app.Context;
import ohos.media.audio.AudioManager;
import ohos.media.audio.AudioRemoteException;

import static ohos.media.audio.AudioManager.AudioVolumeType.STREAM_MUSIC;

/**
 * @ClassName VolumeStore
 * @Author cxs
 * @Version 1.0
 * @Description TODO
 * @Date 2022/03/26
 */
public class VolumeStore {
    private volatile static VolumeStore instance = null;
    private final Context context;
    private static int maxVolume;
    private static int currentVolume;
    private final AudioManager audioManager;;

    private VolumeStore(Context context) {
        this.context = context;
        audioManager = new AudioManager(context);
    }

    public static VolumeStore getInstance(Context context) {
        if (instance == null) {
            synchronized (VolumeStore.class) {
                if (instance == null) {
                    instance = new VolumeStore(context);
                }
            }
        }
        return instance;
    }


    public int getMaxVolume() {
        try {
            maxVolume = audioManager.getMaxVolume(STREAM_MUSIC);
        } catch (AudioRemoteException e) {
            e.printStackTrace();
        }
        return maxVolume;
    }

    public int getVolume() {
        try {
            currentVolume = audioManager.getVolume(STREAM_MUSIC);
        } catch (AudioRemoteException e) {
            e.printStackTrace();
        }
        return currentVolume;
    }

    public void setCurrentVolume(int volume) {
        audioManager.setVolume(STREAM_MUSIC, volume);
    }
}
